/**Trigger Name: NMROpportunityShareTrg
 * Author: Accenture
 * Requirement/Project Name: NMR
 * Requirement Description: Opportunity should be shared with Secondary Owner
 */
trigger NMROpportunityShareTrg on Opportunity (after insert,after update) {
    Id nmropptyRT = RecordTypeHelper.GetRecordTypeID('Opportunity', 'NMR Opportunity');
    Map<Id,Id> ownerIdMap = new Map<Id,Id>();
    Map<Id,Id> ownerIdMapDelete = new Map<Id,Id>();
    for(Opportunity oppty:Trigger.new){
      if(oppty.recordtypeId==nmropptyRT){
      if(oppty.OwnerId != oppty.Secondary_Owner__c){
        if((Trigger.ISINSERT || (Trigger.oldmap.get(oppty.Id).Secondary_Owner__c != oppty.Secondary_Owner__c))){
            if(oppty.Secondary_Owner__c != NULL){
                ownerIdMap.put(oppty.Id,oppty.Secondary_Owner__c);
            }
            if(Trigger.ISUPDATE){
                if(Trigger.oldmap.get(oppty.Id).Secondary_Owner__c != NULL){
                    ownerIdMapDelete.put(oppty.Id,Trigger.oldmap.get(oppty.Id).Secondary_Owner__c);
                }
            }
        }
       }
      }
    }
    if(ownerIdMap.size()>0 || ownerIdMapDelete.size()>0){
         NMRCreateAccOpptyShare.createShareOppty(ownerIdMap,ownerIdMapDelete);
    }
}